package com.ty.bigevent.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ty.bigevent.mapper.CategoryMapper;
import com.ty.bigevent.model.dto.AddArticleRequest;
import com.ty.bigevent.model.dto.UpdateArticleRequest;
import com.ty.bigevent.model.entry.Article;
import com.ty.bigevent.model.entry.Category;
import com.ty.bigevent.service.ArticleService;
import com.ty.bigevent.mapper.ArticleMapper;
import com.ty.bigevent.service.CategoryService;
import com.ty.bigevent.utils.PageBean;
import com.ty.bigevent.utils.ThreadLocalUtil;
import lombok.AllArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
* @author ong char
* @description 针对表【article】的数据库操作Service实现
* @createDate 2025-09-27 00:58:11
*/
@Service
@AllArgsConstructor
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article>
    implements ArticleService{
    private final ArticleMapper articleMapper;
    private final CategoryMapper categoryMapper;

    @Override
    public void addArticle(AddArticleRequest addArticleRequest) {
        Map<String,Object> map =ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        Article article = new Article();
        article.setTitle(addArticleRequest.getTitle());
        article.setContent(addArticleRequest.getContent());
        article.setCover_img(addArticleRequest.getCoverImg());
        article.setState(addArticleRequest.getState());
        article.setCategory_id(addArticleRequest.getCategoryId());
        article.setCreate_user(userId);
        articleMapper.addArticle(article);
    }

    @Override
    public void updateArticle(UpdateArticleRequest updateArticleRequest) {
        Category category = categoryMapper.selectById(updateArticleRequest.getCategoryId());
        if (category == null) {
            throw new RuntimeException("分类不存在");
        }
        Article article = new Article();
        article.setId(updateArticleRequest.getId());
        article.setTitle(updateArticleRequest.getTitle());
        article.setContent(updateArticleRequest.getContent());
        article.setCover_img(updateArticleRequest.getCoverImg());
        article.setState(updateArticleRequest.getState());
        article.setCategory_id(updateArticleRequest.getCategoryId());
        articleMapper.updateArticle(article);
    }

    @Override
    public PageBean<Article> listArticle(Integer pageNum, Integer pageSize, Integer categoryId, String state) {
        Page<Article> page = new Page<>(pageNum, pageSize);
        Map<String,Object> map = ThreadLocalUtil.get();
        Integer userId = (Integer) map.get("id");
        IPage<Article> result = articleMapper.listArticle(page, userId, categoryId, state);
        PageBean<Article> pageBean = new PageBean<>();
        pageBean.setTotal(result.getTotal());
        pageBean.setItems(result.getRecords());
        return pageBean;
    }

    @Override
    public Article getArticleById(Integer id) {
        Article article = articleMapper.getArticleById(id);
        return article;
    }

    @Override
    public void removeArticleById(Integer id) {
        articleMapper.deleteArticleById(id);
    }
}




